package com.second.app.thread.reentrant.conditionmorecorrect;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gyz
 * @date 2021/2/2 9:47
 */
public class MyService {
    /**
     * 唤醒指定线程
     */
    private final ReentrantLock lock = new ReentrantLock();

    public Condition conditionA = lock.newCondition();

    public Condition conditionB = lock.newCondition();

    public void awaitA() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "：开始等待" + System.currentTimeMillis());
            conditionA.await();
            System.out.println(Thread.currentThread().getName() + "：结束等待" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void awaitB() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "：开始等待" + System.currentTimeMillis());
            conditionB.await();
            System.out.println(Thread.currentThread().getName() + "：结束等待" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void signalA() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "：开始唤醒" + System.currentTimeMillis());
            conditionA.signalAll();
        } finally {
            lock.unlock();
        }

    }

    public void signalB() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "：开始唤醒" + System.currentTimeMillis());
            conditionB.signalAll();
        } finally {
            lock.unlock();
        }

    }


}
